unit ClsCodFiscal;

interface

uses DbTables, dm, biblio, sysutils;

type
  TCodFiscal = class(TObject)
  private
    CodF_codigo: string;
  public
    CodF_Descricao: string;
    CodF_Tipo: string;
    CodF_Classificacao: string;
    function Gravar(p_CodF_codigo: string): Byte;
    function Recuperar(p_CodF_codigo: string): Byte;
    function Excluir(p_CodF_codigo: string): Byte;
    function Ultimo: string;
    function Primeiro: string;
    procedure Consultar(var p_q: TQuery; p_CodF_Descricao: string);
    procedure FiltrarModeloCFOP(var p_q: TQuery; p_Mod_codigo: string);

  end;

implementation

{ TCodFiscal }

procedure TCodFiscal.Consultar(var p_q: TQuery; p_CodF_Descricao: string);
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from CodigoFiscal where CodF_Descricao>=:k_CodF_Descricao order by CodF_descricao');
  p_q.parambyname('k_CodF_Descricao').asstring := p_CodF_Descricao;
end;

procedure TCodFiscal.FiltrarModeloCFOP(var p_q: TQuery; p_Mod_codigo: string);
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('select m.codf_codigo,m.mod_codigo,f.codf_tipo, f.codf_descricao from modelo_codigofiscal m');
  p_q.sql.add('left join codigofiscal f on f.codf_codigo = m.codf_codigo ');
  p_q.sql.add('where m.mod_codigo = :k_Mod_Codigo order by CodF_descricao');

  p_q.parambyname('k_Mod_Codigo').asstring := p_Mod_codigo;
end;

function TCodFiscal.Excluir(p_CodF_codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM CodigoFiscal WHERE CodF_Codigo=:k_CodF_Codigo');
  tq.parambyname('k_CodF_Codigo').asstring := trim(p_CodF_Codigo);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

function TCodFiscal.Gravar(p_CodF_codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM CodigoFiscal where CodF_Codigo=:k_CodF_Codigo');
  tq.parambyname('k_CodF_Codigo').asstring := trim(p_CodF_Codigo);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO CodigoFiscal VALUES(:k_CodF_Codigo,:k_CodF_tipo,:k_CodF_Descricao,:k_CodF_Classificacao)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE CodigoFiscal SET CodF_tipo=:k_CodF_tipo,CodF_Descricao=:k_CodF_Descricao,CodF_Classificacao=:k_CodF_Classificacao WHERE CodF_Codigo=:k_CodF_Codigo');
  end;
  tq.parambyname('k_CodF_Codigo').asstring := trim(p_CodF_Codigo);
  tq.parambyname('k_CodF_Tipo').asstring := CodF_Tipo;
  tq.parambyname('k_CodF_Descricao').asstring := CodF_Descricao;
  tq.ParamByName('k_CodF_Classificacao').asstring := CodF_Classificacao;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TCodFiscal.Recuperar(p_CodF_codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM CodigoFiscal where CodF_Codigo=:k_CodF_Codigo');
  tq.Params[0].asstring := trim(p_CodF_Codigo);
  tq.open;
  CodF_Codigo := trim(p_CodF_Codigo);
  if tq.eof then begin
    CodF_Codigo := '';
    CodF_Descricao := '';
    CodF_Tipo := '';
    CodF_Classificacao := '';
    Result := 0;
  end else begin
    CodF_Codigo := tq.fieldbyname('CodF_Codigo').asstring;
    CodF_Descricao := tq.fieldbyname('CodF_Descricao').AsString;
    CodF_Tipo := tq.fieldbyname('CodF_Tipo').asstring;
    CodF_Classificacao := tq.fieldbyname('CodF_Classificacao').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TCodFiscal.Ultimo: string;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('Select max(CodF_Codigo) maior from CodigoFiscal');
  tq.open;
  Result := tq.fieldbyname('maior').asstring;
  tq.Close;
  tq.Free;
end;

function TCodFiscal.Primeiro: string;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('Select min(CodF_Codigo) menor from CodigoFiscal');
  tq.open;
  Result := tq.fieldbyname('menor').asstring;
  tq.Close;
  tq.Free;
end;

end.
